#include "utility.h"
#include <stdio.h>
#include <string.h>
#include <math.h>

const int CHANCES = 3;

void check_int(int answer)
{
    int in = 0;
    int times = 0;
    printf("请输入答案（整数，如 2，你有 %d 次机会）：", CHANCES);
    scanf("%d", &in);
    while (in != answer && ++times < CHANCES)
    {
        printf("错误 %d 次，请再次输入答案：", times);
        scanf("%d", &in);
    }

    if (times == CHANCES)
    {
        printf("你出错 %d 次，正确答案：%d\n", CHANCES, answer);
    }
}

void check_float(float answer)
{
    float in = 0;
    printf("请输入答案（浮点数，如 2.5）：");
    scanf("%f", &in);
    while (fabs(in - answer) > 0.001)
    {
        printf("错误，请再次输入答案：");
        scanf("%f", &in);
    }
}

void check_str(const char* answer)
{
    char in[64];
    int times = 0;
    printf("请输入答案（字符串，如 hello，你有 %d 次机会）：", CHANCES);
    scanf("%s", &in);
    while (strcmp(in, answer) && ++times < CHANCES)
    {
        printf("错误 %d 次，请再次输入答案：", times);
        scanf("%s", &in);
    }

    if (times == CHANCES)
    {
        printf("你出错 %d 次，正确答案：%s\n", CHANCES, answer);
    }
}
